'use strict'; //严格模式
// 登录
app.controller('signin', ['$scope', '$http', '$state', '$rootScope', '$document', '$cookies',
    function($scope, $http, $state, $rootScope, $document, $cookies) {
        // 存放用户信息
        $scope.user = {};

        // 存放验证码图片的src
        $scope.url = $rootScope.baseUrl + "AdminUser/verifyCode";

        // 点击换验证码
        $scope.authCode = function() {
            var str = $scope.getRandom(3);
            // 根据获取到的随机数来刷新图片
            $scope.url = $scope.url + "?" + str;

            $scope.user.code = "";
        }

        var $app = $(".app");
        $app.css("background", "#666"); //初始化app的背景

        // 判断是否有登录
        // $http({
        //     url: $rootScope.baseUrl + "AdminUser/checkLogined",
        //     method: "post",
        // }).then(function(res) {
        //         var code = res.data.code;
        //         if (code == 200) {
        //             if (res.data.data.is_logined) {
        //                 $state.go('fullScore.examPaperManage');
        //             }
        //         } else if (res.data.code == 409) {
        //             $state.go("access.signin");
        //         }
        //     },
        //     function(error) {
        //         $loginBtn.removeClass("btnActive").val("登录");
        //         $scope.errorPrompt = "服务器错误";
        //     });

        var $loginBtn = $("#loginBtn");
        //点击登录按钮执行的函数
        $scope.login = function() {
            if ($scope.user.username == undefined) {
                $scope.errorPrompt = "登录账号不能为空";
            } else if ($scope.user.password == undefined) {
                $scope.errorPrompt = "密码不能为空";
            } else if ($scope.user.code == undefined) {
                $scope.errorPrompt = "请输入验证码";
            } else {
                $loginBtn.addClass("btnActive").val("登录中");

                $http({
                    url: $rootScope.baseUrl + "AdminUser/login",
                    method: "post",
                    data: {
                        "username": $scope.user.username,
                        "password": $scope.user.password,
                        "code": $scope.user.code
                    }
                }).then(function(res) {
                        var code = res.data.code;
                        if (code == 200) {
                            $cookies.loginName = $scope.user.username;
                            $app.removeClass("btnActive"); //初始化app的背景

                            // 登录成功,跳转页面
                            $state.go('fullScore.examPaperManage');
                        } else if (code == 3002 || code == 3001) {
                            $scope.errorPrompt = res.data.msg;
                            $loginBtn.removeClass("btnActive").val("登录");
                            $scope.authCode();
                        } else if (res.data.code == 409) {
                            $state.go('access.signin');
                        } else if (res.data.code == 303) {
                            swal({
                                    title: res.data.msg,
                                    type: "warning",
                                    showCancelButton: true,
                                    confirmButtonColor: "#c40000",
                                    confirmButtonText: "确定",
                                    closeOnConfirm: true
                                },
                                function() {
                                    $state.go('access.signin');
                                });
                        }
                    },
                    function(error) {
                        $loginBtn.removeClass("btnActive").val("登录");
                        $scope.errorPrompt = "服务器错误";
                    });
            }
        };

        $document.keypress(function(event) {
            if (event.keyCode == 13) {
                $scope.login(); //点击enter登录
            }
        });

        // 获取随机数
        $scope.getRandom = function(num) {
            var str = "";
            var strNum = "0123456789abcdefghilklmnopqrstuvwxyz";
            for (var i = 0; i < num; i++) {
                str += strNum.substr(parseInt(Math.random() * strNum.length), 1);
            }
            return str;
        }
    }
]);